"use strict";
cc._RF.push(module, 'b6568vkio1MZrh30fs5UFq9', 'Vehicle');
// cases/entity/Vehicle.ts

"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var Smoother_1 = require("../Smoother");
var BaseEntity_1 = require("./BaseEntity");
var SteeringBehaviors_1 = require("./SteeringBehaviors");
var ccclass = cc._decorator.ccclass;
var tempVec2 = cc.v2();
var tempVec22 = cc.v2();
var Vehicle = /** @class */ (function (_super) {
    __extends(Vehicle, _super);
    function Vehicle() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        //速度
        _this._velocity = cc.v2();
        //朝向
        _this._heading = cc.v2(0, 1);
        _this._world = null;
        //朝向的垂直方向向量
        _this._side = cc.v2(-1, 0);
        //智能体最大速度值
        _this._maxSpeed = 150;
        //智能体可以产生最大的驱动力
        _this._maxForce = 2 * 200;
        //智能体可以产生的最大旋转力
        _this._maxTurnRate = Math.PI * 2;
        _this._steering = null;
        _this._timeElapsed = 0;
        _this._headingSmoother = null; //多次采样来确定方向,防止频繁两头摆
        _this._smootherHead = cc.v2(0, 1);
        _this.isSmoother = true;
        _this.ischange = false;
        return _this;
    }
    Vehicle_1 = Vehicle;
    Object.defineProperty(Vehicle.prototype, "velocity", {
        get: function () {
            return this._velocity;
        },
        set: function (value) {
            this._velocity.set(value);
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(Vehicle.prototype, "heading", {
        get: function () {
            return this._heading;
        },
        set: function (value) {
            if (value.magSqr() < 0.00001) {
                console.error("new heading is zero", this.ID);
                return;
            }
            this._heading.set(value);
            Vehicle_1.Vec2Perp(this._side, this._heading);
            if (!this.isSmoother) {
                var angle = this._heading.signAngle(cc.Vec2.UP);
                var degress = cc.misc.radiansToDegrees(angle);
                if (this.node) {
                    this.node.angle = -degress;
                }
            }
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(Vehicle.prototype, "world", {
        get: function () {
            return this._world;
        },
        enumerable: false,
        configurable: true
    });
    Vehicle.prototype.setWorld = function (value) {
        this._world = value;
    };
    Object.defineProperty(Vehicle.prototype, "side", {
        get: function () {
            return this._side;
        },
        set: function (value) {
            this._side = value;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(Vehicle.prototype, "maxSpeed", {
        get: function () {
            return this._maxSpeed;
        },
        set: function (value) {
            this._maxSpeed = value;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(Vehicle.prototype, "maxForce", {
        get: function () {
            return this._maxForce;
        },
        set: function (value) {
            this._maxForce = value;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(Vehicle.prototype, "maxTurnRate", {
        get: function () {
            return this._maxTurnRate;
        },
        set: function (value) {
            this._maxTurnRate = value;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(Vehicle.prototype, "steering", {
        get: function () {
            return this._steering;
        },
        enumerable: false,
        configurable: true
    });
    Vehicle.prototype.timeElapse = function () {
        return this._timeElapsed;
    };
    Vehicle.prototype.init = function () {
        _super.prototype.init.call(this);
        if (!this._steering) {
            this._steering = new SteeringBehaviors_1.default(this);
        }
        if (!this._headingSmoother) {
            this._headingSmoother = new Smoother_1.Smoother(10);
        }
        Vehicle_1.halfWidth = cc.winSize.width * 0.5;
        Vehicle_1.halfHeight = cc.winSize.height * 0.5;
    };
    Object.defineProperty(Vehicle.prototype, "smootherHead", {
        get: function () {
            return this._smootherHead;
        },
        set: function (value) {
            this._smootherHead.set(value);
            var angle = this._smootherHead.signAngle(cc.Vec2.UP);
            var degress = cc.misc.radiansToDegrees(angle);
            if (this.node) {
                this.node.angle = -degress;
            }
        },
        enumerable: false,
        configurable: true
    });
    Vehicle.prototype.updateDir = function (dt) {
        this.smootherHead = this._headingSmoother.onUpdate(this._heading);
    };
    Vehicle.prototype.update = function (dt) {
        if (!this.isPause) {
            this._timeElapsed = dt;
        }
        this.ischange = false;
        var force = this.steering.calculate();
        cc.Vec2.multiplyScalar(tempVec2, force, dt);
        cc.Vec2.add(tempVec2, tempVec2, this.velocity);
        Vehicle_1.Vec2Truncate(tempVec2, this.maxSpeed);
        this.velocity = tempVec2;
        cc.Vec2.multiplyScalar(tempVec2, this.velocity, dt);
        cc.Vec2.set(tempVec22, this.node.x, this.node.y);
        cc.Vec2.add(tempVec2, tempVec22, tempVec2);
        this.node.setPosition(tempVec2);
        if (this.velocity.magSqr() > 0.0001) {
            cc.Vec2.normalize(tempVec2, this.velocity);
            this.heading = tempVec2;
        }
        if (this.isSmoother)
            this.updateDir(dt);
        if (this.node.x > Vehicle_1.halfWidth) {
            this.node.x = -Vehicle_1.halfWidth + this.node.x - Vehicle_1.halfWidth;
            this.ischange = true;
        }
        if (this.node.x < -Vehicle_1.halfWidth) {
            this.node.x = Vehicle_1.halfWidth + this.node.x + Vehicle_1.halfWidth;
            this.ischange = true;
        }
        if (this.node.y > Vehicle_1.halfHeight) {
            this.node.y = -Vehicle_1.halfHeight + this.node.y - Vehicle_1.halfHeight;
            this.ischange = true;
        }
        if (this.node.y < -Vehicle_1.halfHeight) {
            this.node.y = Vehicle_1.halfHeight + this.node.y + Vehicle_1.halfHeight;
            this.ischange = true;
        }
    };
    Vehicle.Vec2Perp = function (out, a) {
        cc.Vec2.set(out, -a.y, a.x);
        return out;
    };
    Vehicle.Vec2Truncate = function (out, max) {
        if (max <= 0)
            return cc.Vec2.ZERO;
        if (out.magSqr() > max * max) {
            cc.Vec2.normalize(out, out);
            cc.Vec2.multiplyScalar(out, out, max);
        }
        return out;
    };
    var Vehicle_1;
    Vehicle.halfWidth = cc.winSize.width * 0.5;
    Vehicle.halfHeight = cc.winSize.height * 0.5;
    Vehicle = Vehicle_1 = __decorate([
        ccclass
    ], Vehicle);
    return Vehicle;
}(BaseEntity_1.default));
exports.default = Vehicle;

cc._RF.pop();